// pages/search/search.js
import utils from '../../utils/utils'
import req from '../../utils/req'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollable: true,
    showCondition: false,
    condition: ['人气最高', '距离最近', '性价比高'],
    people: ['儿童', '少年', '青年', '中年', '老年'],
    chooseItem: {
      peopleIdx: -1
    },
    others: '',
    peopleNum: '',
    minBudget: '',
    maxBudget: '',
    height: 0,
    page: 1,
    size: 10,
    totalStrategy: '',
    strategy: [],
    taps: [],
    selectPicker: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    that.getTaps();
    that.search('search')
    wx.getSystemInfo({
      success(res) {
        that.setData({
          height: res.windowHeight
        })
      }
    })
  },
  chooseCondition(e) {
    that.setData({
      conditionIdx: e.currentTarget.dataset.index
    })
  },
  changeItem(e) {
    const chooseItem = this.data.chooseItem;
    const type = e.currentTarget.dataset.type;
    const idx = e.currentTarget.dataset.index;
    chooseItem[type] = idx;
    this.setData({
      chooseItem: chooseItem
    })
  },
  handleClickShowCondition() {
    this.setData({
      showCondition: !this.data.showCondition
    })
  },
  getpeopleNum(e) {
    this.setData({
      peopleNum: e.detail.value
    })
  },
  getOthers(e) {
    this.setData({
      others: e.detail.value
    })
  },
  getMinPrice(e) {
    this.setData({
      minBudget: e.detail.value
    })
  },
  getMaxPrice(e) {
    this.setData({
      maxBudget: e.detail.value
    })
  },
  submit() {
    if ((this.data.minBudget !== '' && this.data.maxBudget === '') || (this.data.minBudget === '' && this.data.maxBudget !== '')) {
      utils.toast('none', '请补充价格范围')
      return
    }
    if (parseInt(this.data.minBudget) >= parseInt(this.data.maxBudget)) {
      utils.toast('none', '价格范围错误')
      return
    }
    this.search('search')
    this.setData({
      showCondition: false
    })
  },
  reset() {
    this.setData({
      chooseItem: {
        peopleIdx: -1
      },
      selectPicker: '',
      peopleNum: '',
      minBudget: '',
      maxBudget: '',
      others: '',
      strategy: []
    })
    this.search("search")
  },
  getsearchKey() {
    let taps = this.data.selectPicker
    this.data.others !== '' ? taps += this.data.others : ''
    this.data.chooseItem.peopleIdx !== -1 ? taps += this.data.people[this.data.chooseItem['peopleIdx']] : ''
    const data = {}
    this.data.minBudget === '0' ? data['minBudget'] = 0 : parseInt(this.data.minBudget) ? data['minBudget'] = parseInt(this.data.minBudget) : ''
    parseInt(this.data.maxBudget) ? data['maxBudget'] = parseInt(this.data.maxBudget) : ''
    taps !== '' ? data['taps'] = taps : ''
    parseInt(this.data.peopleNum) ? data['people'] = parseInt(this.data.peopleNum) : ''
    return data
  },
  search(type) {
    type === 'search' ? this.setData({
      strategy: []
    }) : ''
    const data = this.getsearchKey()
    data['page'] = this.data.page;
    req.get('加载中', '/moment/getStrategy', data, (res) => {
      const data = res.data
      data.list.forEach(e1 => {
        const images = []
        let taps = e1['taps'].split(',')
        e1['images'].forEach(e2 => {
          images.push(e2.url)
        })
        e1.images = images
        e1.taps = taps
        e1['createTime'] = utils.timeFormat(e1['createTime'])
      });
      this.setData({
        totalStrategy: data.total,
        strategy: this.data.strategy.concat(data.list)
      })
    })
  },

  navToDetail(e) {
    let url = '../detail/detail?sid=' + e.currentTarget.dataset.sid
    wx.navigateTo({
      url
    })
  },
  bindPickerChange(e) {
    this.setData({
      selectPicker: this.data.taps[e.detail.value]
    })
  },
  getTaps() {
    req.get('加载中', '/moment/getTaps', {}, (res) => {
      let taps = ''
      res.data.forEach(v => {
        taps === '' ? taps += v.taps : taps += ',' + v.taps
      })
      let set = new Set(taps.split(','));
      this.setData({
        taps: Array.from(set)
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  // onPullDownRefresh: function () {

  // },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})